package com.yootk;
import  java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.*;
class Ticket{
    private int count = 3;
    private Lock lock = new ReentrantLock();
    public void sale(){
        this.lock.lock();
        try{
            if (this.count > 0){
                TimeUnit.SECONDS.sleep(1);
                System.out.println("【" + Thread.currentThread().getName() +"】卖票，剩余票数：" + this.count--);
            }else {
                System.out.println("【" + Thread.currentThread().getName() +"】票已经卖完了，明天再来！");
            }
        }catch (Exception e){
        }finally {
            this.lock.unlock();
        }
    }
}
public class YootkDemo3 {
    public static void main(String[] args) throws Exception{
        Ticket ticet = new Ticket();
        for (int x = 0; x < 5; x++){
            new Thread(() -> {
                ticet.sale();
            },"售票员 - " + x).start();
        }
    }
}
